package com.shujia

import scala.io.{BufferedSource, Source}



object Demo19Implicit03 {
  /**
   * 如果class声明为隐式转换类，则构造方法只能接收一个参数p
   * 可以动态的给参数p对应的类型 所有的变量增加类中定义方法以及属性
   */
  implicit class Demo19Implicit03(path: String) {
    var list:List[String] = _
    def readFileFromPath(): List[String] = {
      val bs: BufferedSource = Source
        .fromFile(path)
      val resList: List[String] = bs.getLines().toList

      bs.close()

      resList
    }

    def printFile():Unit = {
      list = readFileFromPath()
      list.foreach(println)
    }
  }

  def main(args: Array[String]): Unit = {
    // 3、隐式转换类

    // 加载students.txt数据
    new Demo19Implicit03("scala/data/stu/students.txt").readFileFromPath().foreach(println)
    new Demo19Implicit03("scala/data/stu/score.txt").readFileFromPath().foreach(println)
    new Demo19Implicit03("scala/data/stu/subject.txt").readFileFromPath().foreach(println)

    "scala/data/stu/subject.txt".readFileFromPath().foreach(println)
    "scala/data/stu/subject.txt".printFile()
  }

}
